package com.tarena.security.admin.persistis.repository;


import com.tarena.security.admin.dao.UserRoleMapper;
import com.tarena.security.admin.entity.db.UserRole;
import com.tarena.security.admin.exception.SecurityAdminBusinessException;
import com.tarena.security.admin.param.UserRoleParam;
import com.tarena.security.admin.persists.repository.UserRoleRepository;
import com.tarena.security.admin.query.UserRoleQuery;
import io.swagger.models.auth.In;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import static com.tarena.security.admin.errorenum.ResultEnum.SYSTEM_ERROR;
import static com.tarena.security.admin.errorenum.ResultEnum.SYS_USER_NON_EXISTENT;

/**
 * <p>
 * user_role Mapper 接口
 * </p>
 *
 * @author Ly
 * @since 2023-04-10
 */
@Repository
@Slf4j
public class UserRoleRepositoryImpl implements UserRoleRepository{
    @Autowired
    UserRoleMapper userRoleMapper;

//    //插入用户角色信息
//    int insertOne (UserRole userRole);
    @Override
    public int insertOne(UserRole userRole) {
        Integer num = userRoleMapper.insertOne(userRole);
        if (num<1) {
            throw new SecurityAdminBusinessException(SYSTEM_ERROR);
        }
        return num;
    }
    //    //查询用户角色信息
//    UserRole selectOne (Integer userId);
    @Override
    public UserRole selectOne(Integer userId) {
        UserRole userRole= userRoleMapper.selectOne(userId);
        log.info("userRole,{}",userRole);
        if (userRole==null){
            throw new SecurityAdminBusinessException(SYSTEM_ERROR);
        }
        return userRole;
    }

    @Override
    public int deteleOneByUserId(Integer userId) {
        Integer num = userRoleMapper.deleteOneByUserId(userId);
        if (num<1) {
            throw new SecurityAdminBusinessException(SYSTEM_ERROR);
        }
        return num;
    }

    @Override
    public UserRoleQuery selectById(Integer Id) {
        UserRoleQuery userRoleQuery= userRoleMapper.selectById(Id);
        log.info("userRole,{}",userRoleQuery);
        if (userRoleQuery==null){
            throw new SecurityAdminBusinessException(SYS_USER_NON_EXISTENT);
        }
        return userRoleQuery;
    }

    @Override
    public int updateByuserRole(UserRole userRole) {
        Integer num = userRoleMapper.updateByuserRole(userRole);
        if (num<1) {
            throw new SecurityAdminBusinessException(SYSTEM_ERROR);
        }
        return num;
    }


}
